package com.leenmvc.core.utils.dictionary;

import com.leenmvc.core.cache.CacheFactory;
import com.leenmvc.core.enums.CacheType;
import com.leenmvc.core.security.entity.core.SysDict;
import com.leenmvc.core.utils.ConvertUtils;

import java.util.List;
import java.util.Map;

/**
 * 字典表的工具方法类，引用的是DictGetImpl
 *
 */
public class Dict {

    private static DictGetImpl dict = DictGetImpl.getInstance();

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static String value(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        return String.valueOf(typeMap.get(name));
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Object obj(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        return typeMap.get(name);
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Boolean bool(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Boolean.class);
        return (boolean)dictValue;
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Boolean bool(String type, String name, boolean defaultValue) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        if(typeMap.get(name) == null) return defaultValue;
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Boolean.class);
        return (boolean)dictValue;
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Integer intValue(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Integer.class);
        return (int)dictValue;
    }


    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Integer intValue(String type, String name, Integer defaultValue) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        if(typeMap.get(name) == null) return defaultValue;
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Integer.class);
        return (int)dictValue;
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Float floatValue(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Float.class);
        return (float)dictValue;
    }

    /**
     * 直接从缓存中获取字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static Double doubleValue(String type, String name) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        Object dictValue = ConvertUtils.convertType(typeMap.get(name), Double.class);
        return (Double)dictValue;
    }

    /**
     * 更新缓存中字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static void updateValue(String type, String name, Object value) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        if(typeMap == null ) return;
        typeMap.put(name, value);
        if(CacheFactory.getStatus(name) != null) {
           CacheFactory.putStatus(name, Boolean.parseBoolean(String.valueOf(value)));
        }
    }

    /**
     * 更新缓存中字典表记录
     *
     * @param type
     * @param name
     * @return
     */
    public static void insertValue(String type, String name, Object value) {
        Map<String, Object> typeMap = CacheFactory.get(CacheType.GLOBAL_VARIABLE, type);
        if(typeMap == null) return;
        typeMap.put(name, value);
        if("true".equals(String.valueOf(value)) || "false".equals(String.valueOf(value))) {
            CacheFactory.putStatus(name, Boolean.parseBoolean(value.toString()));
        }
    }

    /**
     * 根据字典ID获取字典记录
     *
     * @param id
     * @return
     */
    public static SysDict getById(Object id) {
        return dict.dictId(id);
    }

    /**
     * 根据字典类型获取字典记录列表
     *
     * @param dictType
     * @return
     */
    public static List<SysDict> getByType(String dictType) {
         return dict.dictType(dictType);
    }

    /**
     * 根据字典类型获取字典记录列表
     *
     * @param dictType
     * @return
     */
    public static Map<String, Object> getMapByType(String dictType) {
        return dict.getMapByType(dictType);
    }

    /**
     * 同一类型的不包含pid的列表
     *
     * @param dictType
     * @return
     */
    public static List<SysDict> typeListRidPid(String dictType) {
        return dict.typeListRidPid(dictType);
    }

    /**
     * 根据父ID获取
     *
     * @param pid
     * @return
     */
    public static List<SysDict> getByPid(Object pid) {
        return dict.dictPid(pid);
    }

    /**
     * 根据字典类型和名字获取字典记录
     *
     * @param type
     * @param name
     * @return
     */
    public static SysDict get(String type, String name) {
        return dict.typeNameDict(type, name);
    }

    /**
     * 根据字典类型和名字获取字典值
     *
     * @param type
     * @param name
     * @return
     */
    public static String typeNameGet(String type, String name) {
        return dict.typeNameGet(type, name);
    }


    /**
     * 根据字典类型和名字获取字典值
     *
     * @param type
     * @param name
     * @return
     */
    public static Integer typeNameGetInt(String type, String name) {
        return Integer.valueOf(dict.typeNameGet(type, name));
    }

    /**
     * 插入一条数据到字典表
     *
     * @param sysDict
     * @return
     */
    public static boolean dictInsert(SysDict sysDict) {
        return dict.dictInsert(sysDict);
    }

    /**
     * 修改字典表
     *
     * @param sysDict
     * @return
     */
    public static boolean dictUpdate(SysDict sysDict) {
        return dict.dictUpdate(sysDict);
    }


    /**
     * 删除字典表记录
     *
     * @param sysDict
     * @return
     */
    public static boolean dictDelete(SysDict sysDict) {
        return dict.dictDelete(sysDict);
    }


}